(function($, undefined){

    $.fn.stream = function (options){
        var name='Stream',
            isMethodCall = typeof options === "string",
            args = Array.prototype.slice.call(arguments, 1),
            returnValue = this;

        options = !isMethodCall && args.length ?
            $.extend.apply(null, [true, options].concat(args)) : options;

        if(isMethodCall && options.charAt(0)==="_"){
            return returnValue;
        }

        if(isMethodCall){
            this.each(function(){
                var instance = $data(this, name),
                    methodValue = instance && $.isFunction(instance[options]) ?
                        instance[options].apply(instance, args) : instance;
                if(methodValue !== instance && methodValue !== undefined){
                    returnValue = methodValue;
                    return false;
                }
            });
        } else{
            this.each(function(){
                var instance = $.data(this, name);
                if(instance){
                    instance.option(options || {});
                } else{
                    $.data(this, name, new $.stream(options, this));
                }
            });
        }

        return returnValue;
    };

    $.stream = function(options, element){
        var that = this;
        if(arguments.length){
            this.element = $(element);
            this.options = $.extend(true, {}, this.options, options);
            this.element.bind('remove.stream', function(){
                that.destroy();
            });
            this._init();
        }
    };

    $.stream.PUBLIC_STREAM = 'public_stream';
    $.stream.MY_STREAM = 'my_stream';
    $.stream.USER_STREAM = 'user_stream';
    $.stream.REQUEST_INTERVAL = 10000;

    $.stream.event = {
        
    };

    $.stream.prototype = {
        item_template : '@item.mu@',

        options: {
            type    : '',
            items   : [],
            uid     : null
        },

        _init: function(){
            this.getNewItems();
            this._initBind();
        },

        destroy: function(){},

        _initBind: function(){
            this._bindHandler = this.element.delegate('.clickable', 'click', this._onClickHandle);
        },

        getNewItems: function(){
            var url, data, offsetID, that;
            that = this;
            url = 'index.php/api/stream/'+this.options.type;
            offsetID = this.options.items.length? this.options.items[0]['item_id']: null;
            data = {
                uid      : this.options.uid,
                offsetID : offsetID
            };
            $.ajax({
                url: url,
                cache: true,
                type: 'GET',
                data: data,
                dataType: 'json',
                success: function(response) {
                    var newItems;
                    newItems = response;

                    if(newItems.length){
                        that._appendItems(newItems);
                    }
                },
                error: function(e) {
                    // TODO error handler
                    alert('request error');
                }
            });

        },

        _appendItems: function (items) {
            var leftItemsList = this.element.find('.left-column'),
                rightItemsList = this.element.find('.right-column'),
                timeLine = this.element.find('.timeline'),
                maxHeight,
                itemInner = '',
                that = this;
            if(!leftItemsList || !rightItemsList || !timeLine) {
                return;
            }
            $.each(items, function(k, v) {
                itemInner = $.mustache(that.item_template, v);
                if(leftItemsList.height()<=rightItemsList.height()){
                    leftItemsList.append(itemInner);
                } else {
                    rightItemsList.append(itemInner);
                }
            });
            maxHeight = leftItemsList>rightItemsList? leftItemsList.height(): rightItemsList.height();
            timeLine.height(maxHeight);
            this.options.items = items;
        },

        _onClickHandle: function(e) {
            var curTarget = e.currentTarget;
			if(curTarget.hasClass('zoom-detail')){
				//this._zoomDetail();
			} else if(curTarget.hasClass('comment')){
				//this._showCommentsList();
			} else if(curTarget.hasClass('collect')){
				//this._collectItem();
			} else if(curTarget,hasClass('vote-btn')){
				//this._selectOPtion();
			} else if(curTarget.hasClass('comment-comment')){
				//this._createComment();
			}
        }
    };
})(jQuery);
